import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import unittest
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from A3mall_unittest.unittest_a3mall_db import Mydb
from ddt import ddt, data, unpack, file_data

class A3mall2(unittest.TestCase):

    def setUp(self) -> None:
        # 连接数据库
        self.db = Mydb('10.59.9.14', 'root', 'yidong666', 'a3mall', '3306')
        self.driver = webdriver.Firefox()
        self.driver.maximize_window()
        self.driver.get('http://10.59.9.14:7003/')
        # 隐式等待
        self.driver.implicitly_wait(10)

    def tearDown(self) -> None:
        # 关闭数据库连接
        self.db.close()
        time.sleep(5)
        # 退出浏览器
        self.driver.quit()

    # 用户总金额
    userprice = None

    @file_data('./login.json')
    def test_a3_01(self, username, password, casename, expected):
        try:
            print(casename+expected)
            # 1、点击登录
            self.driver.find_element(By.XPATH, '//a[@href="/home/login.html"]').click()
            # 断言1：会员登录
            dy1 = self.driver.find_element(By.CLASS_NAME, 'reg-new').text
            print(f'dy1:{dy1}')
            self.assertEqual('会员登录', dy1)
            # 2、输入用户名
            self.driver.find_element(By.NAME, 'username').send_keys(username)
            # 3、输入密码
            self.driver.find_element(By.NAME, 'password').send_keys(password)
            # 4、勾选记住密码
            self.driver.find_element(By.CSS_SELECTOR, '#theForm > div.agree-clause > span > label > input').click()
            # 强制等待10秒，输入验证码
            time.sleep(10)
            # 5、点击登录按钮
            self.driver.find_element(By.CLASS_NAME, 'finish-btn').click()
            time.sleep(5)
            # 断言2：欢迎回来，小高4
            dy2 = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[1]').text
            print(f'dy2:{dy2}')
            self.assertIn('欢迎回来', dy2)
            price = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[4]/span').text
            price1 = str(price).split('¥ ')
            print(f"用户总金额:{price1[1]}")
            A3mall2.userprice = float(price1[1])

            # 前置条件 ：账号已登录
            self.test_a3_01()
            time.sleep(1)
            # 点击秒杀
            self.driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/ul/li[4]/a').click()
            time.sleep(1)
            # 浏览器返回事件
            self.driver.back()
            time.sleep(2)
            # 浏览器前进事件
            self.driver.forward()
            time.sleep(2)
            # 浏览器刷新事件
            self.driver.refresh()
            time.sleep(2)
            # 双击要秒杀的商品
            sp1 = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[2]/div[1]/ul/li[2]/div[1]/em/a')
            # 鼠标双击事件
            ActionChains(self.driver).double_click(sp1).perform()
            # 断言3：断言商品详情的商品名称：测试444
            # 隐式等待
            self.driver.implicitly_wait(5)
            dy3 = self.driver.find_element(By.XPATH, '//*[@id="goodsForm"]/div/div[1]/span').text
            print(f'dy3:{dy3}')
            self.assertEqual('测试444', dy3)
            # 选择购买的商品数量
            num = self.driver.find_element(By.XPATH,
                                           '//*[@id="store_nums"]')
            num.clear()
            time.sleep(1)
            num.send_keys('2')
            # 点击立即购买
            self.driver.find_element(By.ID, 'buy-cart').click()
            # 隐式等待
            self.driver.implicitly_wait(5)
            # 断言4：断言进入确认订单页面
            dy4 = self.driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/a[2]').text
            print(f'dy4:{dy4}')
            self.assertEqual('确认订单', dy4)
            # 隐式等待
            self.driver.implicitly_wait(5)
            # 断言5：断言收货地址信息包含：15985645484
            dy5 = self.driver.find_element(By.XPATH, '//*[@id="theForm"]/div[1]/div[2]/div/label').text
            print(f'dy5:{dy5}')
            self.assertIn('15985645484', dy5)
            # 点击下一步
            self.driver.find_element(By.ID, 'order-button').click()
            # 隐式等待
            self.driver.implicitly_wait(5)
            # 断言6：断言进入订单提交页面
            dy6 = self.driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/a[2]').text
            print(f'dy6:{dy6}')
            self.assertEqual('订单完成', dy6)
            # 隐式等待
            self.driver.implicitly_wait(5)
            # 断言7：断言提交订单成功
            dy7 = self.driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/span').text
            print(f'dy7:{dy7}')
            self.assertIn('支付成功', dy7)
            # 6、点击查看订单
            self.driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/p/a').click()
            # 隐式等待
            self.driver.implicitly_wait(5)
            # 断言8：断言进入订单详情页面
            dy8 = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div/div[1]/span').text
            print(f'dy8:{dy8}')
            self.assertEqual('我的订单', dy8)
            # 隐式等待
            self.driver.implicitly_wait(5)
            # 断言9：断言生成了订单号
            dy9 = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[2]/td[2]').text
            print(f'day9:{dy9}')
            self.assertTrue(dy9)
            # 隐式等待
            self.driver.implicitly_wait(5)
            # 断言10：数据库断言：用户总金额-订单支付金额=用户余额
            # 订单支付金额
            yf_price = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div/div[5]/div/div/div[2]/span').text
            orderprice = float(str(yf_price).split('￥￥')[1])
            print(f"订单支付金额：{orderprice}")
            # 用户余额
            amount = self.db.seatch(f"select amount from mall_users where mobile = '{15985645484}'")[0][0]
            print(f"用户余额：{amount}")
            self.assertEqual(str(A3mall2.userprice - orderprice), str(amount))
        except Exception as e:
            print(f'异常信息：{e}')



if __name__ == '__main__':
    unittest.main()